"""
弃用，改用extract_content_from_html.py
"""

from bs4 import BeautifulSoup
import shutil
import re
import os
import csv

# 创建csv文件
def open_csv(name):
    return csv.writer(open("csvs/{}.csv".format(name), "w", newline=""), doublequote=False, escapechar="\\")

def get_content_of_doc(html_path):

    file_name = os.path.splitext(os.path.split(html_path)[1])[0]
    # print(file_name)
    # htmldoc = open("D:/Chrome/Dl/python362/library/math.html","r", encoding="utf-8", errors="ignore").read()
    with open(html_path,"r", encoding="utf-8", errors="ignore") as html_doc:
        soup = BeautifulSoup(html_doc, "lxml")

    # dl标签————函数信息
    taglist = soup.find_all("dl", attrs={"class":"function"})
    # print(taglist)
    
    # 创建csv文件
    csv_file = open_csv(file_name)
    csv_file.writerow(["Fun_name", "Args", "Fun_form", "Description"])

    for tag in taglist:
        # 获取函数名
        fun_name = tag.find("dt").get("id")
        # print(fun_name)

        # 获取函数的参数
        args = tag.find("dt").find_all("em")
        # print(args)
        args_list = []
        for arg in args:
            args_list.append(arg.string)
        # print(args_list)

        # 完整函数形式
        fun_form = ""
        items = tag.find("dt") 
        for item in items:
            fun_form += item.string
        # print(fun_form)

        # 描述内容
        item = tag.find("dd")
        des_content = item.get_text()
        des_content = des_content.replace("\n"," ")
        # print(des_content)
        # os.system("pause")
        csv_file.writerow([fun_name,
                            args_list,
                            fun_form,
                            des_content])



try:
    shutil.rmtree("csvs")
except:
    pass

os.mkdir("csvs")

get_content_of_doc("D:/Chrome/Dl/python362/library/math.html")